<script setup lang="ts">
import { nextTick, reactive, ref } from "vue";
import { addNews, editNews } from "@/services/newsApi.ts";
import JinDrawer from "@/components/framework/jin-drawer/jin-drawer.vue";
import type { FormRule } from "@/types/commonType.ts";
import { FormInstance, Message } from "@arco-design/web-vue";
import useFormSubmit from "@/hooks/useFormSubmit.ts";
import type { newsListType, newsAddType, newsEditType } from "@/types/newsType";

const props = defineProps({
  title: String,
  dataType: Number,
});

const formRef = ref<FormInstance>();

const loading = ref(false);

const handleSubmit = useFormSubmit(formRef);

const emits = defineEmits<{
  (e: "list"): void;
}>();

const drawer = ref<{
  init: () => void;
  close: () => void;
}>();

const options = reactive({
  title: "",
  type: 0,
});

const formModel = reactive<newsEditType>({
  id: "",
  title: "",
  content: "",
  dataType: props.dataType,
  status: 0,
  browseCount: 0,
  introduction: "",
});

const rules = reactive<FormRule>({
  title: {
    required: true,
    message: "请输入标题",
  },
  content: {
    required: true,
    message: "请输入内容",
  },
  introduction: {
    required: true,
    message: "请输入简介",
  },
});

const handleCancel = () => {
  drawer.value?.close();
};

const promise = (url: any, msg: string) => {
  url
    .then((res: any) => {
      if (res.code === 200) {
        Message.success(msg);
        handleCancel();
        emits("list");
      } else {
        Message.warning(res.msg);
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
const editorRef = ref();

const handleOk = () => {
  loading.value = true;
  if (editorRef.value) {
    editorRef.value.init(formModel.content);
  }
  const user = JSON.parse(JSON.stringify(formModel));
  if (options.type === 0) {
    delete user.id;
    promise(addNews(user), "新增成功");
  }
  if (options.type === 1) {
    promise(editNews(user), "修改成功");
  }
};

const show = () => {
  options.type = 0;
  formModel.title = "";
  formModel.id = "";
  formModel.content = "";
  formModel.introduction = "";
  formModel.dataType = props.dataType;
  formModel.status = 0;
  editorRef.value.clear();
  drawer.value?.init();
  loading.value = false;
  formRef.value?.clearValidate();
};

const edit = (data: newsListType) => {
  options.type = 1;
  formModel.title = data.title;
  formModel.id = data.id;
  formModel.content = data.content;
  formModel.introduction = data.introduction;
  formModel.dataType = data.dataType;
  editorRef.value.init(data.content);
  formModel.status = data.status;
  loading.value = false;
  drawer.value?.init();
  formRef.value?.clearValidate();
};

defineExpose({
  show,
  edit,
});
</script>

<template>
  <jin-drawer
    ref="drawer"
    :title="props.title"
    :width="600"
    @ok="handleOk"
    @cancel="handleCancel"
    @beforeOk="handleSubmit"
    :loading="loading"
    :ok-loading="loading"
  >
    <a-form
      auto-label-width
      :model="formModel"
      :rules="rules"
      ref="formRef"
    >
      <a-form-item
        label="标题"
        field="title"
      >
        <a-input
          v-model="formModel.title"
          allow-clear
          placeholder="请输入标题"
        />
      </a-form-item>

      <a-form-item
        label="简介"
        field="introduction"
      >
        <a-textarea
          placeholder="请输入简介"
          allow-clear
          v-model="formModel.introduction"
          :auto-size="{minRows:2,maxRows:5}"
        />
      </a-form-item>

      <a-form-item
        label="内容"
        field="content"
      >
        <editor-content
          ref="editorRef"
          v-model="formModel.content"
          @update:modelValue="handleChange"
        />
      </a-form-item>

      <a-form-item
        label="状态"
        field="status"
      >
        <a-radio-group
          v-model="formModel.status"

        >
          <a-radio :value="0">草稿</a-radio>
          <a-radio :value="1">发布</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </jin-drawer>
</template>

<style scoped lang="scss">
</style>